/**
 * 
 */
package yanivosher.aiproject;

import java.util.ArrayList;
import java.util.LinkedList;

/**
 * @author Yanivl1
 * 
 */
public class TerroristBuster extends Agent {

	public TerroristBuster(Vertex currentVertex, Vertex destinationVertex) {
		super(currentVertex, destinationVertex);
	}

	public Step makeAmove(Graph curr) {
		Step step = new Step();
		DijkstraAlgorithm algo = new DijkstraAlgorithm(Environment.getInstance().graph);
		ArrayList<Vertex> blockedVertexes;
		if(currentVertex.getMilitary()>0) 
		{
			System.out.println("currentVertex.getMilitary()>0 == " + currentVertex.getMilitary());
			blockedVertexes = Environment.getInstance().graph.getBlockedVertexes();
		}
		else
		{
			System.out.println("currentVertex.getMilitary()!=0 == " + currentVertex.getMilitary());
			blockedVertexes = Environment.getInstance().graph.getMilitaryVertexes();
		}
		
		if (blockedVertexes.size() > 0) {
			Vertex destVertex = currentVertex;
			int cost = -1;
			
			for (Vertex vertex : blockedVertexes) {
				System.out.println("Want to go to vertex " + vertex.getName());
				algo.execute(currentVertex);
				LinkedList<Vertex> path = algo.getPath(vertex);
				Environment.printPath(path);
				if (path == null || path.size() < 2) {
					System.out.println("Dijkstra trip null! continue...");
					continue;
				}
				else
				{
					int cost2 = Environment.getInstance().graph.getPathCost(path);
					if (((cost > cost2) && (cost2 > 0)) || cost == -1) {
						destVertex = path.get(1);
						cost = cost2;
					}
				}
			}
			System.out.println("set next vertex " + destVertex.getName());
			step.setDestination(destVertex);
			step.setMilitary(currentVertex.getMilitary()>0 ? true : false);
			step.setCurrent(currentVertex);
			step.setChem(false);
		}
		else {
			step.setDestination(currentVertex);
			step.setMilitary(false);
			step.setCurrent(currentVertex);
			step.setChem(false);
		}
		return step;
		//Check if you on militry point.
		//If yes:
		//1. take m. unit
		//2. search for nearest blocked edge
		//
		//To find cheapest blocked edge ->
		//get a list of all the blocked vertex (if (a,b) is blocked edge so a & b called blocked vertexes)
		//Run dijkstra to all the blocked vertexes.
		//Get the minimum cost vertex.
		//
		//Else: 
		//Find the nearest vertex with militry.
		//Go for it.
	}

	@Override
	public boolean idDone() {
		// TODO Auto-generated method stub
		return Environment.getInstance().graph.getBlockedVertexes().size() == 0 || Environment.getInstance().graph.getMilitaryVertexes().size() == 0;
	}
}
